package com.demo.connectdbtest.module.generatetype;

import java.util.*;

public class GenerateType {

    public void testOne() {
        //在集合中使用泛型
        ArrayList<Integer> list = new ArrayList<>();
        list.add(78);
        list.add(88);

        for (Integer item : list) {
            //不需要强转
            System.out.println(item);
        }
        Iterator<Integer> iterator = list.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }

        HashMap<String, Integer> map = new HashMap<>();
        map.put("one", 1);
        map.put("two", 2);
        Collection<Integer> values = map.values();
        Set<String> strings = map.keySet();
        //遍历map
        Set<Map.Entry<String, Integer>> entries = map.entrySet();
        Iterator<Map.Entry<String, Integer>> iterator1 = entries.iterator();
        while (iterator1.hasNext()) {
            Map.Entry<String, Integer> entries1 = iterator1.next();
            System.out.println("key=" + entries1.getKey() + "value=" + entries1.getValue());
        }
    }

    public static void main(String[] args) {
        ArrayList list = new ArrayList<>();
        list.add("hello");
        test(list);//泛型擦除 编译不会类型检查
        ArrayList<Object> objects = new ArrayList<>();
        //test(objects);//爆红 一旦指定object 编译会类型检查，必须按照object处理
    }

    public static void test(ArrayList<String> list) {
        String str = "";
        for (String s : list) {
            str += s + ",";
        }
        System.out.println(str + "元素");
    }
}
